package cn.swust.util;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class DBUtil {
	
	
	private static String user;
	private static String driver="com.mysql.jdbc.Driver";
	private static String url ;
	private static String password;
	
	/**
 * 	链接数据库
 * @return Connection
 */
	public static Connection getConnection()
	{
		Properties p = new Properties();
		try {
			p.load(DBUtil.class.getResourceAsStream("/jdbc.properties"));
			
		} catch (IOException e2) {
			System.out.println(DBUtil.class+"找不到数据库配置属性文件");
			e2.printStackTrace();
		}
			url= p.getProperty("database");
			user= p.getProperty("loginname");
			password = p.getProperty("loginpwd");			
		
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, user, password);				
		} catch (ClassNotFoundException e) {
			System.out.println("驱动加载错！");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("数据库链接出错！");
			e.printStackTrace();
		}
		return conn;
	}
	
	/**
	 * 创建sql语句执行对象
	 * @param Connection
	 * @return Statement
	 */
	public static Statement creatStatement(Connection conn)
	{
		Statement sta = null;
		try {
			sta = conn.createStatement();
		} catch (SQLException e) {
			System.out.println("创建sql语句执行对象错误！");
			e.printStackTrace();
		}
		return sta;
	}
	/**
	 * 执行查询sql语句
	 * @param sql
	 * @param stmt
	 * @return ResultSet
	 */
	public static ResultSet querySql(String sql,Statement stmt)
	{
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql);
		} catch (SQLException e) {
			System.out.println("执行查询sql语句错误！");
			e.printStackTrace();
		}
		return rs;
	}
	/**
	 * 执行更新sql语句
	 * @param sql
	 * @param stmt
	 * @return 1/0
	 */
	public static int updateSql(String sql)
	{ 
		int ret = 0;
		Connection conn=DBUtil.getConnection();
		Statement stmt = null;		
		try {
			stmt = conn.createStatement();
			ret = stmt.executeUpdate(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			close(stmt);
			close(conn);
		}
		return ret;
	}
	/**
	 * 关闭sql执行对象
	 * @param stmt
	 */
	public static void close(Statement stmt)
	{
		if(stmt!=null)
		{try {
			stmt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}}
	}
	/**
	 * 关闭结果集
	 * @param rs
	 */
	public static void close(ResultSet rs)
	{
		if(rs != null)
		{
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		rs = null;
	}
	/**
	 * 关闭数据库链接
	 * @param conn
	 */
	public static void close(Connection conn)
	{
		if(conn!=null)
			try {
				conn.close();
			} catch (SQLException e) {
				
				e.printStackTrace();
			}
	}
	/**
	 * 关闭DBUtil
	 * @param conn
	 * @param stmt
	 * @param rs
	 */
	public static void close(Connection conn,Statement stmt,ResultSet rs)
	{
		close(rs);
		close(stmt);
		close(conn);
	}
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String [] args)
	{
		Connection conn=DBUtil.getConnection();
		Statement stmt=DBUtil.creatStatement(conn);
		String sql="select count(*) from admin";
		ResultSet rs=DBUtil.querySql(sql, stmt);
		try {
			while(rs.next())
			{
				System.out.println(rs.getInt(1));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DBUtil.close(conn, stmt, rs);
	}
	public static String getElement(String key)
	{
		Properties p = new Properties();
		try {
			p.load(DBUtil.class.getResourceAsStream("/jdbc.properties"));
			
		} catch (IOException e2) {
			System.out.println(DBUtil.class+"找不到数据库配置属性文件");
			e2.printStackTrace();
		}
		return p.getProperty(key);
	}
}
